package com.latvia.ui;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.latvia.cookingapp.R;
import com.latvia.model.Recipe;

public class RecipesListViewAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ArrayList<Recipe> items;
	private Context context;

	public RecipesListViewAdapter(Context context, ArrayList<Recipe> items) {
		this.context = context;
		this.items = items;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		 
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return items.get(position).hashCode();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (convertView == null) {
			row = inflater.inflate(R.layout.all_recipe_item, null);
		}

		final Recipe recipe = items.get(position);

		ImageView imageView = (ImageView) row
				.findViewById(R.id.item_recipeIcon);
		imageView.setImageResource(recipe.getIconId());

		TextView makeText = (TextView) row.findViewById(R.id.item_recipeName);
		makeText.setText(recipe.getName());

		TextView yearText = (TextView) row
				.findViewById(R.id.item_recipeDescription);
		yearText.setText(recipe.getDescription());
		
		 final ImageView heartImageView = (ImageView)row.findViewById(R.id.item_favouriteIcon);
		 heartImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 if (recipe.isFavourite()) {
					 heartImageView.setImageResource(R.drawable.btn_heart);
				 } else {
					 heartImageView.setImageResource(R.drawable.btn_heart_a);
				 }
				 
				 recipe.setFavourite(!recipe.isFavourite());
				
			}
		});
		 
		 if (recipe.isFavourite()) {
			 heartImageView.setImageResource(R.drawable.btn_heart_a);
		 } else {
			 heartImageView.setImageResource(R.drawable.btn_heart);
		 }
		 
		 
		 TextView recipeLevelTextView = (TextView)row.findViewById(R.id.item_recipe_level);
		 String text = null;
		 int color = R.color.Green;
		 switch (recipe.getLevel()) {
		case 1:
			text = "E";
			color = R.color.Green;
			break;
		case 2:
			text = "M";
			color = R.color.Yellow;
			break;
		case 3:
			text = "H";
			color = R.color.Red;
			break;
		default:
			break;
		}
		 
		 recipeLevelTextView.setText(text);
		 recipeLevelTextView.setTextColor(context.getResources().getColor(color));

		return row;

	}



}
